package com.dataextend.exception;


import com.dataextend.bean.ReturnResult;
import com.dataextend.bean.BusinessException;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import org.slf4j.Logger;

@RestControllerAdvice
public class RestExceptionHandler {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     *  捕获程序中发生的Exception异常
     */
    @ExceptionHandler(Exception.class)
    public ReturnResult restExceptionHandler(Exception e){
        logger.error("调用失败",e);
        ReturnResult returnResult = new ReturnResult();
        returnResult.setCode("-1");
        returnResult.setMsg("调用失败");
        returnResult.setSuccess(false);
        return returnResult;
    }

    /**
     *  捕获程序中发生的BusinessException异常
     */
    @ExceptionHandler(BusinessException.class)
    public ReturnResult restExceptionHandler(BusinessException e){
        logger.error(e.getCode(),e);
        ReturnResult returnResult = new ReturnResult();
        returnResult.setCode("-1");
        returnResult.setMsg("业务异常");
        returnResult.setSuccess(false);
        return returnResult;
    }



}
